﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace per.esmool.utils {

    public static class StringKit {

        public static string JoinEQWithValue(string seperator, params KeyValuePair<string, object>[] tokens)
            => Join(seperator, tokens.Select(kvp => $"{kvp.Key}={kvp.Value}").ToArray());

        public static string Join(string seperator, params object[] tokens) {
            StringJoiner joiner = new(seperator);
            foreach (var token in tokens)
                joiner.Add(token.ToString());
            return joiner.ToString();
        }

    }

    public class StringJoiner(string separator) {

        readonly string seperator = separator;
        readonly StringBuilder builder = new();

        bool needComma = false;

        public void Add(string token) {
            if (this.needComma)
                this.builder.Append(this.seperator);
            this.needComma = true;
            this.builder.Append(token);
        }

        public override string ToString() {
            return this.builder.ToString();
        }
    }
}
